#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="PathCameraController.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Render.
//
//   starLiGHT.Render is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Render. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Render are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Camera.TwoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using starLiGHT.Collections;

    #endregion

    public class PathCameraController : ICameraController
    {
//////////TODO:        LockFreeQueue<Vector2> pathPositions;
        private Queue<Vector2> pathPositions;
        private Vector2 lastPosition;
        private Vector2 nextPosition;
        private float speed = 250.00f;
        private float t = 0.0f;
        private bool firstRun;
        private float len;

        public PathCameraController()
        {
            this.pathPositions = new Queue<Vector2>();
        }

        public string Name
        {
            get { return "Path"; }
        }

        public void AddPosition(Vector2 nextPosition)
        {
            this.pathPositions.Enqueue(nextPosition);
        }

        public void Update(GameTime gameTime, Camera2D camera)
        {
            if (this.lastPosition == this.nextPosition && this.pathPositions.Count < 1)
            {
                return;
            }

            if (this.firstRun)
            {
                this.nextPosition = this.pathPositions.Dequeue();
                this.lastPosition = camera.Anchor;
                this.len = (camera.Anchor - this.nextPosition).Length();
                this.firstRun = false;
            }

            this.t += this.speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (this.t >= this.len && this.pathPositions.Count > 0)
            {
                this.lastPosition = camera.Anchor;
                this.nextPosition = this.pathPositions.Dequeue();
                this.len = (camera.Anchor - this.nextPosition).Length();
                this.t = 0;
            }
            else if (this.t >= this.len)
            {
                return;
            }

            camera.Anchor = Vector2.Lerp(this.lastPosition, this.nextPosition, (this.t / this.len));
        }
    }
}